<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>留言管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="7">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="messagelist" stripe>
        <el-table-column label="序号" prop="id" width="50"></el-table-column>
        <el-table-column label="创建时间" prop="create_time" width="150"></el-table-column>
        <el-table-column label="评论昵称" prop="author" width="100"></el-table-column>
        <el-table-column label="邮箱" prop="email" width="150"></el-table-column>
        <el-table-column label="IP地址" prop="ipAddress" width="120"></el-table-column>
        <el-table-column label="个人网站" prop="url" width="150"></el-table-column>
        <el-table-column label="是否可用" width="100">
          <template v-slot:default="scope">
            <el-switch v-model="scope.row.state" @change="messageStateChange(scope.row.id)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="父评论" prop="parent_id" width="80"></el-table-column>
        <el-table-column label="回复" prop="reply" width="130"></el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="scope">
            <el-button type="primary" icon="el-icon-view" @click="view(scope.row.content)"></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="deletemessage(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="size"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <el-dialog
        title="留言详情"
        :visible.sync="viewDialogVisible"
        @close="viewDialogClosed"
        width="50%">
        <!--内容主体区域-->
        <el-form ref="messageFormRef">
            <el-form-item label="留言详情">
              <el-input v-model="message" type="textarea" resize="none" :disabled="true"></el-input>
            </el-form-item>
        </el-form>
        <!--内容底部区域-->
        <span slot="footer" class="dialog-footer">
            <el-button @click="viewDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="viewDialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      messagelist: [],
      current: 0,
      size: 1,
      total: 1,
      viewDialogVisible: false,
      message: ''
    }
  },
  created () {
    this.getMessages()
  },
  methods: {
    handleCurrentChange (currentPage) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/message/getSomeMessages', {
        params: {
          current: currentPage,
          size: 20
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'messagelist', response.data.data.records)
        this.total = response.data.data.total
        this.size = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    messageStateChange (mid) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/message/changeMessageState', {
        params: {
          id: mid
        }
      }).then(response => {
        // console.log(response.data)
        if (response.data.code !== '200') this.$message.error('修改失败')
        else this.$message.success('修改成功')
      }).catch(error => {
        console.log(error)
      })
    },
    viewDialogClosed () {
      this.$refs.messageFormRef.resetFields()
    },
    deletemessage (mid) {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/message/deleteMessage', {
        params: {
          id: mid
        }
      }).then(response => {
        // console.log(response.data)
        if (response.data.code !== '200') this.$message.error('删除失败')
        else this.$message.success('删除成功')
      }).catch(error => {
        console.log(error)
      })
    },
    view (content) {
      this.viewDialogVisible = true
      this.message = content
    },
    getMessages () {
      var token = sessionStorage.getItem('token')
      this.$axios.defaults.headers.Authorization = 'Bearer ' + token
      this.$axios.get('/message/getSomeMessages', {
        params: {
          current: 1,
          size: 20
        }
      }).then(response => {
        // console.log(response.data)
        this.$set(this, 'messagelist', response.data.data.records)
        this.total = response.data.data.total
        this.size = response.data.data.size
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
